class_name MapNodeButton
extends Button

var map_node: MapNode
var glow_tween: Tween
var scale_tween: Tween

func _init():
	# 设置按钮样式
	custom_minimum_size = Vector2(120, 100)
	flat = true
	
	# 添加鼠标进入和离开效果
	mouse_entered.connect(_on_mouse_entered)
	mouse_exited.connect(_on_mouse_exited)
	
	# 确保按钮可以接收点击事件
	mouse_filter = Control.MOUSE_FILTER_PASS
	
	# 添加点击事件调试
	pressed.connect(_on_button_pressed)

func _on_button_pressed():
	print("=== MapNodeButton 点击事件 ===")
	print("MapNodeButton pressed: ", map_node.get_node_name() if map_node else "No node")
	print("Button position: ", position, " size: ", size)
	print("Button mouse filter: ", mouse_filter, " disabled: ", disabled)
	print("Button visible: ", visible)
	print("Button modulate: ", modulate)
	print("Button z_index: ", z_index)
	print("Parent: ", get_parent().name if get_parent() else "No parent")
	print("=== MapNodeButton 点击事件结束 ===")

func setup_node(node: MapNode):
	map_node = node
	_setup_button()

func _setup_button():
	# 设置文本和工具提示
	text = ""
	tooltip_text = map_node.get_node_name() + "\n" + map_node.get_node_description()
	
	# 强制设置鼠标过滤，确保可以接收点击事件
	mouse_filter = Control.MOUSE_FILTER_PASS
	
	# 设置样式
	_update_style()
	
	# 添加图标标签
	_create_icon_label()
	
	# 更新节点状态
	_update_node_state()

func _create_icon_label():
	# 创建图标标签
	var icon_label = Label.new()
	icon_label.name = "IconLabel"
	icon_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	icon_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
	icon_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
	icon_label.size_flags_vertical = Control.SIZE_EXPAND_FILL
	icon_label.add_theme_font_size_override("font_size", 24)
	add_child(icon_label)
	
	# 创建名称标签
	var name_label = Label.new()
	name_label.name = "NameLabel"
	name_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	name_label.vertical_alignment = VERTICAL_ALIGNMENT_BOTTOM
	name_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
	name_label.add_theme_font_size_override("font_size", 12)
	name_label.anchor_top = 0.7
	name_label.anchor_bottom = 1.0
	add_child(name_label)

func _update_style():
	# 检查节点是否存在，如果不存在则创建
	if not has_node("IconLabel") or not has_node("NameLabel"):
		_create_icon_label()
	
	var icon_label = get_node("IconLabel")
	var name_label = get_node("NameLabel")
	
	# 根据节点类型设置图标、颜色和样式
	match map_node.node_type:
		MapNode.NodeType.COMBAT:
			icon_label.text = "⚔️"
			name_label.text = "战斗"
			_set_background_color(Color(0.8, 0.2, 0.2, 0.8))
		MapNode.NodeType.ELITE:
			icon_label.text = "👑"
			name_label.text = "精英"
			_set_background_color(Color(1.0, 0.8, 0.0, 0.8))
		MapNode.NodeType.BOSS:
			icon_label.text = "💀"
			name_label.text = "Boss"
			_set_background_color(Color(0.8, 0.0, 0.8, 0.9))
		MapNode.NodeType.REST:
			icon_label.text = "🏠"
			name_label.text = "汉府"
			_set_background_color(Color(0.2, 0.8, 0.2, 0.8))
		MapNode.NodeType.SHOP:
			icon_label.text = "🛒"
			name_label.text = "吾悦"
			_set_background_color(Color(0.2, 0.2, 0.8, 0.8))
		MapNode.NodeType.EVENT:
			icon_label.text = "❓"
			name_label.text = "事件"
			_set_background_color(Color(1.0, 1.0, 0.2, 0.8))
		MapNode.NodeType.TREASURE:
			icon_label.text = "💎"
			name_label.text = "宝箱"
			_set_background_color(Color(1.0, 0.6, 0.0, 0.8))
		MapNode.NodeType.REMOVE_CARD:
			icon_label.text = "🗑️"
			name_label.text = "删牌"
			_set_background_color(Color(0.8, 0.3, 0.3, 0.8))
		MapNode.NodeType.UPGRADE_CARD:
			icon_label.text = "⬆️"
			name_label.text = "升级"
			_set_background_color(Color(0.2, 0.8, 1.0, 0.8))
		MapNode.NodeType.HEAL_EVENT:
			icon_label.text = "💚"
			name_label.text = "治疗"
			_set_background_color(Color(0.2, 1.0, 0.8, 0.8))
		MapNode.NodeType.RANDOM_CARD:
			icon_label.text = "🎲"
			name_label.text = "卡牌"
			_set_background_color(Color(1.0, 0.8, 0.2, 0.8))
	
	# 设置节点状态
	_update_node_state()

func _set_background_color(color: Color):
	# 创建背景样式
	var style_box = StyleBoxFlat.new()
	style_box.bg_color = color
	style_box.corner_radius_top_left = 15
	style_box.corner_radius_top_right = 15
	style_box.corner_radius_bottom_left = 15
	style_box.corner_radius_bottom_right = 15
	style_box.border_width_left = 2
	style_box.border_width_right = 2
	style_box.border_width_top = 2
	style_box.border_width_bottom = 2
	style_box.border_color = Color.WHITE
	
	add_theme_stylebox_override("normal", style_box)

func _update_node_state():
	# 极简模式：所有按钮始终可点击，先打通交互链路
	disabled = false
	mouse_filter = Control.MOUSE_FILTER_PASS
	modulate = Color.WHITE
	_remove_glow_effect()
	_remove_status_mark()
	
	# 调试信息
	print("Node ", map_node.get_node_name(), " - accessible: ", map_node.is_accessible, ", disabled: ", disabled, ", mouse_filter: ", mouse_filter)

func _add_glow_effect():
	# 添加发光效果
	if glow_tween:
		glow_tween.kill()
	
	glow_tween = create_tween()
	glow_tween.set_loops()
	glow_tween.tween_property(self, "modulate:a", 1.2, 0.5)
	glow_tween.tween_property(self, "modulate:a", 0.8, 0.5)

func _remove_glow_effect():
	# 移除发光效果
	if glow_tween:
		glow_tween.kill()
		glow_tween = null

func _on_mouse_entered():
	if not disabled:
		# 鼠标悬停效果
		if scale_tween:
			scale_tween.kill()
		scale_tween = create_tween()
		scale_tween.tween_property(self, "scale", Vector2(1.1, 1.1), 0.1)

func _on_mouse_exited():
	if not disabled:
		# 鼠标离开效果
		if scale_tween:
			scale_tween.kill()
		scale_tween = create_tween()
		scale_tween.tween_property(self, "scale", Vector2(1.0, 1.0), 0.1)

func _add_completed_mark():
	# 添加完成标记（绿色对勾）
	_remove_status_mark()
	var mark = Label.new()
	mark.name = "StatusMark"
	mark.text = "✓"
	mark.add_theme_font_size_override("font_size", 16)
	mark.add_theme_color_override("font_color", Color.GREEN)
	mark.anchors_preset = Control.PRESET_TOP_RIGHT
	mark.anchor_top = 0.0
	mark.anchor_right = 1.0
	mark.anchor_bottom = 0.3
	mark.offset_left = -20
	mark.offset_top = 5
	mark.offset_right = -5
	mark.offset_bottom = 25
	add_child(mark)

func _add_current_mark():
	# 添加当前位置标记（蓝色圆点）
	_remove_status_mark()
	var mark = Label.new()
	mark.name = "StatusMark"
	mark.text = "●"
	mark.add_theme_font_size_override("font_size", 12)
	mark.add_theme_color_override("font_color", Color.BLUE)
	mark.anchors_preset = Control.PRESET_TOP_RIGHT
	mark.anchor_top = 0.0
	mark.anchor_right = 1.0
	mark.anchor_bottom = 0.3
	mark.offset_left = -15
	mark.offset_top = 5
	mark.offset_right = -5
	mark.offset_bottom = 20
	add_child(mark)

func _add_selectable_mark():
	# 添加可选标记（黄色边框）
	_remove_status_mark()
	var mark = Panel.new()
	mark.name = "StatusMark"
	mark.anchors_preset = Control.PRESET_FULL_RECT
	mark.anchor_right = 1.0
	mark.anchor_bottom = 1.0
	mark.offset_left = -3
	mark.offset_top = -3
	mark.offset_right = 3
	mark.offset_bottom = 3
	
	var style = StyleBoxFlat.new()
	style.bg_color = Color.TRANSPARENT
	style.border_width_left = 2
	style.border_width_right = 2
	style.border_width_top = 2
	style.border_width_bottom = 2
	style.border_color = Color.YELLOW
	style.corner_radius_top_left = 18
	style.corner_radius_top_right = 18
	style.corner_radius_bottom_left = 18
	style.corner_radius_bottom_right = 18
	mark.add_theme_stylebox_override("panel", style)
	
	add_child(mark)

func _add_locked_mark():
	# 添加锁定标记（红色锁）
	_remove_status_mark()
	var mark = Label.new()
	mark.name = "StatusMark"
	mark.text = "🔒"
	mark.add_theme_font_size_override("font_size", 14)
	mark.anchors_preset = Control.PRESET_TOP_RIGHT
	mark.anchor_top = 0.0
	mark.anchor_right = 1.0
	mark.anchor_bottom = 0.3
	mark.offset_left = -20
	mark.offset_top = 5
	mark.offset_right = -5
	mark.offset_bottom = 25
	add_child(mark)

func _remove_status_mark():
	# 移除状态标记
	if has_node("StatusMark"):
		get_node("StatusMark").queue_free()

func update_node(new_node: MapNode):
	map_node = new_node
	tooltip_text = map_node.get_node_name() + "\n" + map_node.get_node_description()
	_update_style()
